package com.example.english.db;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

/**
 * SQLite
 */
public class DBSQLite extends SQLiteOpenHelper{

    private static final String DbName = "english_sl.db";
    private static final int version = 1 ;

    // 创建表结构
    private String CreateTable = "create table user (" +
            "id integer primary key AUTOINCREMENT," +
            "account TEXT ," +
            "password TEXT," +
            "name TEXT)";

    private String word = "create table word (" +
            "id integer primary key AUTOINCREMENT," +
            "english TEXT ," +
            "translate TEXT)";

    public DBSQLite(@Nullable Context context) {
        super(context, DbName, null, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CreateTable);
        db.execSQL(word);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }


    // 添加
    public void add(UserBean user){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("account",user.getAccount());
        values.put("password",user.getPassword());
        values.put("name",user.getName());
        db.insert("user",null,values);
    }

    public void update(String account,String pwd){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("password",pwd);
        String[] whereArgs = {account};
        db.update("user",values,"account=?",whereArgs);
    }

    // 获取所有数据集
    public List<UserBean> getList(){
        SQLiteDatabase db = this.getReadableDatabase();
        List<UserBean> userList = new ArrayList<>();
        if(db == null){
            Log.d("getList","null?");
        }
        Cursor cursor = db.query("user", null, null, null, null, null,
                null);
        while (cursor.moveToNext()){
            UserBean userBean = new UserBean();
            userBean.setAccount(getString(cursor, "account"));
            userBean.setPassword(getString(cursor, "password"));
            userList.add(userBean);
        }
        return userList;
    }

    // 添加生词本
    public void add(WordBean bean){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("english",bean.getEnglish());
        values.put("translate",bean.getTranslate());
        db.insert("word",null,values);
    }

    public void del(WordBean bean){
        SQLiteDatabase db = this.getWritableDatabase();
        String[] whereArgs = {bean.getEnglish()};
        db.delete("word","english=?",whereArgs);
    }

    public List<WordBean> getListWord(){
        SQLiteDatabase db = this.getReadableDatabase();
        List<WordBean> userList = new ArrayList<>();
        if(db == null){
            Log.d("getList","null?");
        }
        Cursor cursor = db.query("word", null, null, null, null, null,
                null);
        while (cursor.moveToNext()){
            WordBean userBean = new WordBean();
            userBean.setEnglish(getString(cursor, "english"));
            userBean.setTranslate(getString(cursor, "translate"));
            userList.add(userBean);
        }
        return userList;
    }


    @SuppressLint("Range")
    private  int getInt(Cursor cursor, String name) {
        return cursor.getInt(cursor.getColumnIndex(name));
    }

    @SuppressLint("Range")
    private  String getString(Cursor cursor, String name) {
        return cursor.getString(cursor.getColumnIndex(name));
    }

}
